function initEnv() {
	
	
	$("body").append(DWZ.frag["dwzFrag"]);

	$(window).resize(function(){
		initLayout();
		$(this).trigger(DWZ.eventType.resizeGrid);
	});

	var ajaxbg = $("#background,#progressBar");
	ajaxbg.hide();
	$(document).ajaxStart(function(){
		ajaxbg.show();
	}).ajaxStop(function(){
		ajaxbg.hide();
	});
	
	if ($.fn.jBar) $("#leftside").jBar({minW:150, maxW:700});
	
	if ($.taskBar) $.taskBar.init();
	if ($.fn.switchEnv) $("#switchEnvBox").switchEnv();
	if ($.fn.navMenu) $("#navMenu").navMenu();
		
	setTimeout(function(){
		initLayout();
		if (window.navTab) navTab.init();

		// 注册DWZ插件。
		DWZ.regPlugins.push(initUI); //第三方jQuery插件注册方法：DWZ.regPlugins.push(function($p){});

		// 首次初始化插件
		$(document).initUI();
		
		// navTab styles
		var jTabsPH = $("div.tabsPageHeader");
		jTabsPH.find(".tabsLeft").hoverClass("tabsLeftHover");
		jTabsPH.find(".tabsRight").hoverClass("tabsRightHover");
		jTabsPH.find(".tabsMore").hoverClass("tabsMoreHover");

		$(document).trigger(DWZ.eventType.initEnvAfter);
	}, 10);

}
function initLayout(){
	var iContentW = $(window).width() - (DWZ.ui.sbar ? $("#sidebar").width() + 10 : 34) - 5;
	var iContentH = $(window).height() - $("#header").height() - 34;

	$("#container").width(iContentW);
	$("#container .tabsPageContent").height(iContentH - 34).find("[layoutH]").layoutH();
	$("#sidebar, #sidebar_s .collapse, #splitBar, #splitBarProxy").height(iContentH - 5);
	$("#taskbar").css({top: iContentH + $("#header").height() + 5, width:$(window).width()});
}

function initUI($p){
	//add必选项提示
	var span = "<span name='requiredSpan' style='color:red;font-weight:bold;height:21px;line-height:21px;' >(※必填项)</span>";
	var $targetP = $(".validRequired", $p);
	$targetP.after(span);
	//tables
	if ($.fn.jTable) {
		var $tb = $("table.table", $p).prop("outerHTML");
		var content = "";
		var contentTh = "";
		var contentB = "";
		if(!$tb){
		//	console.log("666");
			//取出table的宽度,并判断这个宽度值是否存在（数值）,无问题，拼接html
			var widthBefore = parseInt($(".gridHeader table",$p).css("width"));
		//	console.log("widthBefore==>"+widthBefore);
			//取出layoutH的值
			var layoutH = $(".gridScroller",$p).attr("layouth");
			var contentTable = "<table class='table' width='"+[widthBefore+20]+"' layoutH='"+layoutH+"'>";
		//	console.log("contentTable==>"+contentTable);
			//取出各th中tr的宽度值，计算出宽度比例
			var oldTr = $(".gridThead thead>tr",$p).find("th");
			for(var i = 0; i<oldTr.size(); i++){
				var $th = $(oldTr[i]);
				var visible = $th.css("visibility");
				var innerHtmlByTh = $th.prop("outerHTML");
				var display = $th.css("display");
		//		console.log(i+"==class==>"+display);
		//		console.log(i+"==thhtml==>"+innerHtmlByTh);
				var widthBeforByTh = parseInt($(innerHtmlByTh).css("width"));
		//		console.log(i+"==widthBeforByTh==>"+widthBeforByTh);
				var percent = (widthBeforByTh+2)*100/(widthBefore+20-100);
		//		console.log(i+"==percent==>"+percent);
				var title = $(".gridCol",$th).html();
		//		console.log(i+"==thtitle==>"+title);
				/*if(visible=="hidden"){
					contentTh += "<th width='"+percent+"%' style='visibility:hidden;'>"+title+"</th>";
				}else{
					contentTh += "<th width='"+percent+"%'"+">"+title+"</th>";
				}*/
				if(display=="none"){
					contentTh += "<th width='"+percent+"%' style='display:none;'>"+title+"</th>";
				}else{
					contentTh += "<th width='"+percent+"%'"+">"+title+"</th>";
				}
			}
	//		console.log("contentTh==>"+contentTh);
			var contentA = "<thead><tr>"
							+contentTh
							+"</tr></thead>"
	//		console.log("contentA==>"+contentA);
			
			//取出tbody的值
			var $tbody = $(".gridTbody tbody",$p);
			var id = $tbody.attr("id");
			//判断是否有id,如果有，就要拼接
	//		console.log("id==>"+id);
			/*var oldTrByTbody = $(".gridTbody tbody>tr",$p).find("td");
			for(var j = 0; j<oldTrByTbody.size(); j++){
				var $td = $(oldTrByTbody[j]);
				var tdValue = $td.html();
				console.log(j+"==td==>"+tdValue);
				contentTd+="<td>"+tdValue+"</td>";
				console.log("contentTd==>"+contentTd);
			}
		    var contentB = "<tbody id='"+id+"'>"
		    				+"<tr>"
		    				+contentTd
		    				+"<tr>"
		    				+"</tbody>"*/
			$(".gridTbody tbody>tr",$p).each(function(tridex,tritem){
				var oldTdByTbody = $(tritem).find("td");
				var contentTd = "";
				for(var j = 0; j<oldTdByTbody.size(); j++){
					var $td = $(oldTdByTbody[j]);
					var tdValue = $td.html();
			//		var tdVisibility = $td.css("visibility");
					var tdDisplay = $td.css("display");
					var classDisplay = $th.attr("class");
			//		console.log("classDisplay==td==>"+classDisplay);
					if(tdDisplay=="none"){
						contentTd+="<td style='display:none;'>"+tdValue+"</td>";
					}else{
						contentTd+="<td>"+tdValue+"</td>";
					}
		//			console.log("contentTd==>"+contentTd);
				}
				contentB += "<tr>"+contentTd+"</tr>"
				
			});
		//	console.log("contentB==>"+contentB);
		    var content = contentTable +  contentA + "<tbody id='"+id+"'>" + contentB + "</tbody></table>"
		//    console.log("content==>"+content);
		    console.log("grid==>"+$p.find(".grid").remove().html());
		    $p.append(content);
		//    console.log("html====>"+$p.html());
		}
		$("table.table", $p).jTable();
	}
	
	//日期年月的插件20190227 hgl 新增下面3行
	if($.fn.simpleCanleder){
		$(".dateYM").simpleCanleder();
	}
	// css tables
	if ($.fn.cssTable) $('table.list', $p).not('.nowrap').cssTable();

	if ($.fn.jPanel) $("div.panel", $p).jPanel();

	//auto bind tabs
	$("div.tabs", $p).each(function(){
		var $this = $(this);
		var options = {};
		options.currentIndex = $this.attr("currentIndex") || 0;
		options.eventType = $this.attr("eventType") || "click";
		$this.tabs(options);
	});

	if ($.fn.jTree) $("ul.tree", $p).jTree();

	if ($.fn.jTree){
		$('div.accordion', $p).each(function(){
			var $this = $(this);
			$this.accordion({fillSpace:$this.attr("fillSpace"),alwaysOpen:true,active:0});
		});
	}

	if ($.fn.checkboxCtrl){
		$(":button.checkboxCtrl, :checkbox.checkboxCtrl", $p).checkboxCtrl($p);
	}
	
	if ($.fn.combox) $("select.combox",$p).combox();
	
	if ($.fn.xheditor) {
		$("textarea.editor", $p).each(function(){
			var $this = $(this);
			var op = {html5Upload:false, skin: 'vista',tools: $this.attr("tools") || 'full'};
			var upAttrs = [
				["upLinkUrl","upLinkExt","zip,rar,txt"],
				["upImgUrl","upImgExt","jpg,jpeg,gif,png"],
				["upFlashUrl","upFlashExt","swf"],
				["upMediaUrl","upMediaExt","avi"]
			];
			
			$(upAttrs).each(function(i){
				var urlAttr = upAttrs[i][0];
				var extAttr = upAttrs[i][1];
				
				if ($this.attr(urlAttr)) {
					op[urlAttr] = $this.attr(urlAttr);
					op[extAttr] = $this.attr(extAttr) || upAttrs[i][2];
				}
			});
			
			$this.xheditor(op);
		});
	}
	
	if ($.fn.uploadify) {
		$(":file[uploaderOption]", $p).each(function(){
			var $this = $(this);
			var options = {
				fileObjName: $this.attr("name") || "file",
				auto: true,
				multi: true,
				onUploadError: uploadifyError
			};
			
			var uploaderOption = DWZ.jsonEval($this.attr("uploaderOption"));
			$.extend(options, uploaderOption);

			DWZ.debug("uploaderOption: "+DWZ.obj2str(uploaderOption));
			
			$this.uploadify(options);
		});
	}
	
	// init styles
	
	$("input[type=text], input[type=password], textarea", $p).addClass("textInput").focusClass("focus");
	
	

	$("input[readonly], textarea[readonly]", $p).addClass("readonly");
	$("input[disabled=true], textarea[disabled=true]", $p).addClass("disabled");

	$("input[type=text]", $p).not("div.tabs input[type=text]", $p).filter("[alt]").inputAlert();
	
	

	//Grid ToolBar

	$("div.panelBar li, div.panelBar", $p).hoverClass("hover");
	//Button
	$("div.button", $p).hoverClass("buttonHover");
	$("div.buttonActive", $p).hoverClass("buttonActiveHover");
		
	//tabsPageHeader
	$("div.tabsHeader li, div.tabsPageHeader li, div.accordionHeader, div.accordion", $p).hoverClass("hover");


	

	
	//validate form
	if ($.fn.validate) {
		$("form.required-validate", $p).each(function(){
			var $form = $(this);
			$form.validate({
				onsubmit: false,
				focusInvalid: false,
				focusCleanup: true,
				errorElement: "span",
				ignore:".ignore",
				invalidHandler: function(form, validator) {
					var errors = validator.numberOfInvalids();
					if (errors) {
						var message = DWZ.msg("validateFormError",[errors]);
						alertMsg.error(message);
					}
				}
			});

			$form.find('input[customvalid]').each(function(){
				var $input = $(this);
				$input.rules("add", {
					customvalid: $input.attr("customvalid")
				})
			});
		});
	}
    //日历控件
	if ($.fn.datepicker){
		$('input.date', $p).each(function(){
			var $this = $(this);
			var opts = {};
			if ($this.attr("dateFmt")) opts.pattern = $this.attr("dateFmt");
			if ($this.attr("minDate")) opts.minDate = $this.attr("minDate");
			if ($this.attr("maxDate")) opts.maxDate = $this.attr("maxDate");
			if ($this.attr("mmStep")) opts.mmStep = $this.attr("mmStep");
			if ($this.attr("ssStep")) opts.ssStep = $this.attr("ssStep");
			$this.datepicker(opts);
		});
	}

	// navTab
	$("a[target=navTab]", $p).each(function(){
		$(this).click(function(event){
			var $this = $(this);
			//alert("navTab_$this:"+$this.prop("outerHTML"));
			//标签的标题就是<a>的text值，如果有属性title，就显示title的值
			var title = $this.attr("title") || $this.text();
			//tabid页面刷新用的
			var tabid = $this.attr("rel") || "_blank";
			var fresh = eval($this.attr("fresh") || "true");
			//alert("fresh:"+fresh)
			var external = eval($this.attr("external") || "false");
			//alert("href:::::::"+unescape($this.attr("href")));
			
			//20180511 惠国磊  以下一行 
			var param = $this.attr("param")||"";
			console.log("param=====>"+param);
			var url = unescape($this.attr("href")).replaceTmById($(event.target).parents(".unitBox:first"));
			DWZ.debug(url);
			if (!url.isFinishedTm()) {
				alertMsg.error($this.attr("warn") || DWZ.msg("alertSelectMsg"));
				return false;
			}
			
			//20180511  惠国磊   以下一行 
			navTab.openTab(tabid, url,{title:title, fresh:fresh, external:external,data:{param:param}});

			event.preventDefault();
		});
	});

	//dialogs
	$("a[target=dialog]", $p).each(function(){
		$(this).click(function(event){
			var $this = $(this);
			//20180913 hgl 以下4行
			//如果页面有必输项验证不过的，阻止dialog加载
			var flag = $this.attr("flag");
			if(flag=="false"){
				return false;
			}
			var title = $this.attr("title") || $this.text();
			var rel = $this.attr("rel") || "_blank";
			var wh=$this.attr("wh") ;
			var options = {};
			var w = $this.attr("width");
			var h = $this.attr("height");
			if (w) options.width = w;
			if (h) options.height = h;
			if(wh=="big")
			{
				options=getOptionsBig();
			}else if(wh=="middle"){
				options=getOptionsMiddle();
			}else if(wh=="small"){
				options=getOptionsSmall();
			}
			options.max = eval($this.attr("max") || "false");
			options.mask = eval($this.attr("mask") || "false");
			options.maxable = eval($this.attr("maxable") || "true");
			options.minable = eval($this.attr("minable") || "true");
			options.fresh = eval($this.attr("fresh") || "true");
			options.resizable = eval($this.attr("resizable") || "true");
			options.drawable = eval($this.attr("drawable") || "true");
			options.close = eval($this.attr("close") || "");
			options.param = $this.attr("param") || "";
			//alert("options.param:"+options.param);
			options.list = $this.attr("list") || "";
		//	console.log("options.list:"+options.list);
			options.credential_type = $this.attr("credential_type") || "";
			options.credentialNo = $this.attr("credentialNo") || "";
			options.credentialNo = $this.attr("credentialNo") || "";
			options.appType = $this.attr("appType") || "";
			options.pageFlag = $this.attr("pageFlag") || "";
			//20181219 hgl 增加以下一行
			options.ipAddress = $this.attr("ipAddress") || "";
		//	console.log("options.credentialNo:"+options.credentialNo);

			var url = unescape($this.attr("href")).replaceTmById($(event.target).parents(".unitBox:first"));
			DWZ.debug(url);
			if (!url.isFinishedTm()) {
				alertMsg.error($this.attr("warn") || DWZ.msg("alertSelectMsg"));
				return false;
			}
			
			$.pdialog.open(url, rel, title, options);
			
			return false;
		});
	});
	$("a[target=ajax]", $p).each(function(){
		$(this).click(function(event){
			var $this = $(this);
			var rel = $this.attr("rel");
			if (rel) {
				var $rel = $("#"+rel);
				var url = unescape($this.attr("href")).replaceTmById($(event.target).parents(".unitBox:first"));
				DWZ.debug(url);
				if (!url.isFinishedTm()) {
					alertMsg.error($this.attr("warn") || DWZ.msg("alertSelectMsg"));
					return false;
				}

				$rel.loadUrl(url, {}, function(){
					$rel.find("[layoutH]").layoutH();
				});
			}

			event.preventDefault();
		});
	});
	
	$("div.pagination", $p).each(function(){
		//console.log("$this==>"+this.get);
		var $this = $(this);
		//console.log("$this==>"+$this.prop("outerHTML"));
		$this.pagination({
			targetType:$this.attr("targetType"),
			rel:$this.attr("rel"),
			totalCount:$this.attr("totalCount"),
			numPerPage:$this.attr("numPerPage"),
			pageNumShown:$this.attr("pageNumShown") || 10,
			currentPage:$this.attr("currentPage"),
			callback:$this.attr("callback")
		});
	});
	if($.fn.checkInputValueBySearch) $(".subBar button[type='submit']", $p).checkInputValueBySearch();
	if($.fn.checkInputValueBySubmit) $(".formBar button[type='submit'][class='valid']", $p).checkInputValueBySubmit();
	
	if ($.fn.sortDrag) $("div.sortDrag", $p).sortDrag();

	// dwz.ajax.js
	if ($.fn.ajaxTodo) $("a[target=ajaxTodo]", $p).ajaxTodo();
	if ($.fn.dwzExport) $("a[target=dwzExport]", $p).dwzExport();

	if ($.fn.lookup) $("a[lookupGroup]", $p).lookup();
	if ($.fn.multLookup) $("[multLookup]:button", $p).multLookup();
	if ($.fn.suggest) $("input[suggestFields]", $p).suggest();
	if ($.fn.itemDetail) $("table.itemDetail", $p).itemDetail();
	if ($.fn.selectedTodo) $("a[target=selectedTodo]", $p).selectedTodo();
	if ($.fn.pagerForm) $("form[rel=pagerForm]", $p).pagerForm({parentBox:$p});
	
}
